<template>
  <div class="damagedStatistics">
    <x-header class="header" :left-options="{showBack: showHeaderBack,preventGoBack:true}" @on-click-back="goToPage({path:'/'})" ref="header">破损统计</x-header>
    <div class="topHolder"></div>
    <card class="gridDisplay">
      <div class="card-padding" slot="content" style="padding-top: 0;">
        <div class="filterArea dateFilter" ref="filterArea">
          <group-title style="text-align: center;">当前显示日期：{{choosenDate}}<br/></group-title>
          <x-button :show-loading="loadingStatus" :disabled="loadingStatus" @click.native="changeDate(1)" class="inlineBtn">今日</x-button>
          <x-button :show-loading="loadingStatus" :disabled="loadingStatus" @click.native="changeDate(2)" class="inlineBtn">昨日</x-button>
          <a v-if="loadingStatus" class="datePicker"><i class="iconfont icon-rilicalendar107"></i></a>
          <datetime v-else v-model="choosenDate" :start-date="startDate" :end-date="endDate" @on-confirm="changeDate(0)" class="datePicker">
            <i class="iconfont icon-rilicalendar107"></i>
          </datetime>
        </div>
        <h2 class="title">
          ¥ <span v-text="formatCurrency(damagedInfo.total)">0</span>元<br/>
          <small>破损金额合计</small>
        </h2>
        <grid :cols="2" style="text-align: center;">
          <grid-item label="赔偿金额">
            <span class="data">¥ <span v-text="formatCurrency(damagedInfo.compensateTotal)">0</span>元</span>
          </grid-item>
          <grid-item label="赔偿件数">
            <span class="data"><span v-text="damagedInfo.compensateNumber">0</span>件</span>
          </grid-item>
          <grid-item label="维修金额">
            <span class="data">¥ <span v-text="formatCurrency(damagedInfo.maintainTotal)">0</span>元</span>
          </grid-item>
          <grid-item label="维修件数">
            <span class="data"><span v-text="damagedInfo.maintainNumber">0</span>件</span>
          </grid-item>
        </grid>
      </div>
    </card>
    <group title="破损明细：">
      <x-table :cell-bordered="false">
        <thead>
        <tr style="background-color: #F7F7F7">
          <th width="37%">商品名称</th>
          <th width="15%">数量</th>
          <th width="15%">类型</th>
          <th width="33%">金额</th>
        </tr>
        </thead>
        <tbody>
        <tr v-for="(item,index) in damagedInfo.detail" :key="index">
          <td v-text="item.goodName"></td>
          <td v-text="item.number"></td>
          <td v-text="item.type == 0?'赔偿':'维修'"></td>
          <td v-text="'¥ ' + formatCurrency(item.total) + '元'"></td>
        </tr>
        <tr v-if="JSON.stringify(damagedInfo) != '{}' && damagedInfo.detail.length <= 0">
          <td colspan="4">
            <div class="listNoItem">暂无破损明细</div>
          </td>
        </tr>
        </tbody>
      </x-table>
    </group>
    <br/>
    <br/>
  </div>
</template>

<script>
  import { Group, XButton, Card, Grid, GridItem, Datetime, GroupTitle, XTable, numberComma, dateFormat } from 'vux'
  import {getDamagedStatistics} from "../../api";
  import {message,toDecimal2} from "../../common/util";

  export default {
    name:'damagedStatistics',
    data(){
      return{
        showHeaderBack:this.GLOBAL.showHeaderBack,
        loadingStatus:false,
        choosenDate:dateFormat(new Date(), 'YYYY-MM-DD'),
        startDate:'2019-01-01',
        endDate:dateFormat(new Date(), 'YYYY-MM-DD'),
        damagedInfo:{}
      }
    },
    components:{
      Group,
      XButton,
      Card,
      Grid,
      GridItem,
      Datetime,
      GroupTitle,
      XTable
    },
    computed:{
    },
    methods:{
      goToPage(param){
        this.$router.push(param)
      },
      formatCurrency(number){
        return numberComma(toDecimal2(number))
      },
      changeDate(value){
        console.log(value)
        switch (value) {
          case 1:this.choosenDate = dateFormat(new Date(), 'YYYY-MM-DD');break;
          case 2:this.choosenDate = dateFormat(new Date().setDate(new Date().getDate()-1), 'YYYY-MM-DD');break;
          default: break;
        }
        console.log(this.choosenDate)
        this.refreshPage()
      },
      refreshPage(){
        this.loadingStatus = true
        getDamagedStatistics(this.choosenDate).then((res)=>{
          if(res.status == 'success'){
            this.damagedInfo = res.data
          }else if(res.status == 'error') {
            message({message:res.msg,type:'error'})
            console.log('getDamagedStatistics error info:', res.msg)
          }
          this.loadingStatus = false
        }).catch((err)=>{console.log('getDamagedStatistics catch info:',err)})
      }
    },
    mounted(){
      this.refreshPage()
      this.$store.commit('updateLoadingStatus',{isLoading:false})
    }
  }
</script>

<style lang="scss">
  .damagedStatistics{
    .filterArea{
      &.dateFilter{
        margin-bottom: 0.5rem;
        .datePicker{
          float: right;padding: 0;
          &.weui-cell:before{border-top: none;}
          i{font-size: 2rem;color:#535353;}
        }
      }
      .filterItem{margin-top: 0.5rem;}
    }
    .gridDisplay{
      h2.title{
        line-height: 1.8rem;color: #636363;font-size:2rem;text-align: center;margin: 1rem 0;
        small{font-size:1rem;color: #999999;}
      }
      .data{display: inline-block;height:1.5rem;}
    }
  }
</style>
